package core.model
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;

	public class ConfigData
	{
		static public var configUrl:String;
		static public var configData:Object;
		
		public function ConfigData()
		{
		}
		
		static public function readConfigData(url:String = null):*
		{
			if (null === url) url = configUrl;
			var f:File = new File(url);
			if (f.exists === false) {
				f.cancel();
				return null;
			}
			var fs:FileStream = new FileStream();
			fs.open(f, FileMode.READ);
			var b:ByteArray = new ByteArray();
			fs.readBytes(b, 0, fs.bytesAvailable);
			fs.close();
			f.cancel();
			b.position = 0;
			configData = JSON.parse(b.toString());
			return configData;
		}
		
		static public function writeConfigData(data:* = null, url:String = null):Boolean
		{
			if (null === data) data = configData;
			if (null === url) url = configUrl;
			configData = data;
			var str:String;
			try {
				str = JSON.stringify(data);
			} catch (error:Error) {
				return false;
			}
			var f:File = new File(url);
			var fs:FileStream = new FileStream();
			fs.open(f, FileMode.WRITE);
			fs.writeUTFBytes(str);
			fs.close();
			f.cancel();
			return true;
		}
		
	}
}